{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:32:35.962143Z",
     "start_time": "2019-05-29T07:32:34.577793Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pickle\n",
    "from statsmodels.stats.outliers_influence import variance_inflation_factor\n",
    "import statsmodels.api as sm\n",
    "from sklearn import ensemble\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn import datasets\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "import os\n",
    "import pickle\n",
    "import copy\n",
    "\n",
    "pd.set_option('max_columns', 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T08:00:21.431794Z",
     "start_time": "2019-05-29T08:00:20.989661Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1000, 21)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Account Balance</th>\n",
       "      <th>Duration of Credit (month)</th>\n",
       "      <th>Payment Status of Previous Credit</th>\n",
       "      <th>Purpose</th>\n",
       "      <th>Credit Amount</th>\n",
       "      <th>Value Savings/Stocks</th>\n",
       "      <th>Length of current employment</th>\n",
       "      <th>Instalment per cent</th>\n",
       "      <th>Sex &amp; Marital Status</th>\n",
       "      <th>Guarantors</th>\n",
       "      <th>Duration in Current address</th>\n",
       "      <th>Most valuable available asset</th>\n",
       "      <th>Age (years)</th>\n",
       "      <th>Concurrent Credits</th>\n",
       "      <th>Type of apartment</th>\n",
       "      <th>No of Credits at this Bank</th>\n",
       "      <th>Occupation</th>\n",
       "      <th>No of dependents</th>\n",
       "      <th>Telephone</th>\n",
       "      <th>Foreign Worker</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1049</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>21</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2799</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>841</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2122</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2171</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Account Balance  Duration of Credit (month)  \\\n",
       "0                1                          18   \n",
       "1                1                           9   \n",
       "2                2                          12   \n",
       "3                1                          12   \n",
       "4                1                          12   \n",
       "\n",
       "   Payment Status of Previous Credit  Purpose  Credit Amount  \\\n",
       "0                                  4        2           1049   \n",
       "1                                  4        0           2799   \n",
       "2                                  2        9            841   \n",
       "3                                  4        0           2122   \n",
       "4                                  4        0           2171   \n",
       "\n",
       "   Value Savings/Stocks  Length of current employment  Instalment per cent  \\\n",
       "0                     1                             2                    4   \n",
       "1                     1                             3                    2   \n",
       "2                     2                             4                    2   \n",
       "3                     1                             3                    3   \n",
       "4                     1                             3                    4   \n",
       "\n",
       "   Sex & Marital Status  Guarantors  Duration in Current address  \\\n",
       "0                     2           1                            4   \n",
       "1                     3           1                            2   \n",
       "2                     2           1                            4   \n",
       "3                     3           1                            2   \n",
       "4                     3           1                            4   \n",
       "\n",
       "   Most valuable available asset  Age (years)  Concurrent Credits  \\\n",
       "0                              2           21                   3   \n",
       "1                              1           36                   3   \n",
       "2                              1           23                   3   \n",
       "3                              1           39                   3   \n",
       "4                              2           38                   1   \n",
       "\n",
       "   Type of apartment  No of Credits at this Bank  Occupation  \\\n",
       "0                  1                           1           3   \n",
       "1                  1                           2           3   \n",
       "2                  1                           1           2   \n",
       "3                  1                           2           2   \n",
       "4                  2                           2           2   \n",
       "\n",
       "   No of dependents  Telephone  Foreign Worker  y  \n",
       "0                 1          1               1  0  \n",
       "1                 2          1               1  0  \n",
       "2                 1          1               1  0  \n",
       "3                 2          1               2  0  \n",
       "4                 1          1               2  0  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_01_list = \\\n",
    "    [[19,3,'Part Time',1],\n",
    "    [20,1,'Part Time',1],\n",
    "    [21,2,'Part Time',1],\n",
    "    [22,-1,'Part Time',1],\n",
    "    [23,0,'Part Time',1],\n",
    "    [24,5,'Part Time',0],\n",
    "    [25,1,'Part Time',1],\n",
    "    [26,2,'Part Time',1],\n",
    "    [27,1,'Full Time',1],\n",
    "    [28,2,'Full Time',0],\n",
    "    [29,1,'Full Time',0],\n",
    "    [30,2,'Full Time',0],\n",
    "    [33,6,'Full Time',1],\n",
    "    [34,5,'Full Time',0],\n",
    "    [35,6,'Part Time',0],\n",
    "    [36,5,'Part Time',0],\n",
    "    [37,6,'Full Time',0],\n",
    "    [38,5,'Full Time',0],\n",
    "    [48,4,'Full Time',1],\n",
    "    [49,3,'Others',1],\n",
    "    [50,4,'Full Time',0],\n",
    "    [51,3,'Others',0],\n",
    "    [52,4,'Others',0],\n",
    "    [53,3,'Others',0],\n",
    "    [56,-1,'Others',1],\n",
    "    [57,0,'Others',1],\n",
    "    [58,-1,'Others',1],\n",
    "    [59,0,'Others',1],\n",
    "    [60,-1,'Others',0],\n",
    "    [61,0,'Others',0]]\n",
    "    \n",
    "data_02_df = pd.DataFrame(data_01_list)\n",
    "data_02_df.columns = ['Age', 'TaCA', 'ES', 'y']\n",
    "data = data_02_df\n",
    "\n",
    "data_03 = pd.read_excel('German_credit.xlsx')\n",
    "data = data_03.astype(int).rename(columns={'target': 'y'})\n",
    "\n",
    "allFeatures = data.columns\n",
    "print(data.shape)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T08:00:23.161086Z",
     "start_time": "2019-05-29T08:00:23.070022Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Account Balance\n",
      "3               63\n",
      "2              269\n",
      "1              274\n",
      "4              394\n",
      "    Duration of Credit (month)\n",
      "72                           1\n",
      "47                           1\n",
      "40                           1\n",
      "26                           1\n",
      "5                            1\n",
      "54                           2\n",
      "16                           2\n",
      "22                           2\n",
      "28                           3\n",
      "33                           3\n",
      "13                           4\n",
      "14                           4\n",
      "7                            5\n",
      "45                           5\n",
      "39                           5\n",
      "4                            6\n",
      "8                            7\n",
      "20                           8\n",
      "11                           9\n",
      "42                          11\n",
      "60                          13\n",
      "27                          13\n",
      "10                          28\n",
      "21                          30\n",
      "30                          40\n",
      "48                          48\n",
      "9                           49\n",
      "15                          64\n",
      "6                           75\n",
      "36                          83\n",
      "18                         113\n",
      "12                         179\n",
      "24                         184\n",
      "   Payment Status of Previous Credit\n",
      "0                                 40\n",
      "1                                 49\n",
      "3                                 88\n",
      "4                                293\n",
      "2                                530\n",
      "    Purpose\n",
      "8         9\n",
      "10       12\n",
      "4        12\n",
      "5        22\n",
      "6        50\n",
      "9        97\n",
      "1       103\n",
      "2       181\n",
      "0       234\n",
      "3       280\n",
      "       Credit Amount\n",
      "2439               1\n",
      "1943               1\n",
      "3990               1\n",
      "1938               1\n",
      "2214               1\n",
      "5234               1\n",
      "1936               1\n",
      "1934               1\n",
      "3414               1\n",
      "3979               1\n",
      "12169              1\n",
      "1928               1\n",
      "1274               1\n",
      "1927               1\n",
      "1922               1\n",
      "8065               1\n",
      "1965               1\n",
      "1967               1\n",
      "3386               1\n",
      "2327               1\n",
      "4870               1\n",
      "18424              1\n",
      "2032               1\n",
      "10222              1\n",
      "2022               1\n",
      "3973               1\n",
      "1950               1\n",
      "10144              1\n",
      "1953               1\n",
      "15945              1\n",
      "...              ...\n",
      "1082               2\n",
      "3077               2\n",
      "1940               2\n",
      "1924               2\n",
      "976                2\n",
      "1474               2\n",
      "2578               2\n",
      "1597               2\n",
      "701                2\n",
      "433                2\n",
      "2978               2\n",
      "1413               2\n",
      "1533               2\n",
      "2406               2\n",
      "4526               2\n",
      "1409               2\n",
      "3590               2\n",
      "1410               2\n",
      "1386               2\n",
      "1382               2\n",
      "3617               2\n",
      "1374               2\n",
      "2333               2\n",
      "1364               2\n",
      "1553               2\n",
      "1393               3\n",
      "1275               3\n",
      "1478               3\n",
      "1258               3\n",
      "1262               3\n",
      "\n",
      "[923 rows x 1 columns]\n",
      "   Value Savings/Stocks\n",
      "4                    48\n",
      "3                    63\n",
      "2                   103\n",
      "5                   183\n",
      "1                   603\n",
      "   Length of current employment\n",
      "1                            62\n",
      "2                           172\n",
      "4                           174\n",
      "5                           253\n",
      "3                           339\n",
      "   Instalment per cent\n",
      "1                  136\n",
      "3                  157\n",
      "2                  231\n",
      "4                  476\n",
      "   Sex & Marital Status\n",
      "1                    50\n",
      "4                    92\n",
      "2                   310\n",
      "3                   548\n",
      "   Guarantors\n",
      "2          41\n",
      "3          52\n",
      "1         907\n",
      "   Duration in Current address\n",
      "1                          130\n",
      "3                          149\n",
      "2                          308\n",
      "4                          413\n",
      "   Most valuable available asset\n",
      "4                            154\n",
      "2                            232\n",
      "1                            282\n",
      "3                            332\n",
      "    Age (years)\n",
      "70            1\n",
      "75            2\n",
      "19            2\n",
      "62            2\n",
      "59            3\n",
      "56            3\n",
      "68            3\n",
      "67            3\n",
      "74            4\n",
      "66            5\n",
      "65            5\n",
      "64            5\n",
      "58            5\n",
      "60            6\n",
      "61            7\n",
      "53            7\n",
      "51            8\n",
      "63            8\n",
      "55            8\n",
      "57            9\n",
      "52            9\n",
      "50           10\n",
      "54           10\n",
      "48           12\n",
      "49           14\n",
      "20           14\n",
      "21           14\n",
      "45           15\n",
      "47           17\n",
      "44           17\n",
      "43           17\n",
      "41           17\n",
      "46           18\n",
      "39           21\n",
      "42           22\n",
      "38           24\n",
      "40           27\n",
      "22           27\n",
      "37           29\n",
      "34           33\n",
      "33           33\n",
      "32           34\n",
      "29           37\n",
      "31           38\n",
      "36           40\n",
      "30           40\n",
      "25           40\n",
      "35           40\n",
      "28           43\n",
      "24           45\n",
      "23           48\n",
      "26           49\n",
      "27           50\n",
      "   Concurrent Credits\n",
      "2                  47\n",
      "1                 139\n",
      "3                 814\n",
      "   Type of apartment\n",
      "3                107\n",
      "1                179\n",
      "2                714\n",
      "   No of Credits at this Bank\n",
      "4                           6\n",
      "3                          28\n",
      "2                         333\n",
      "1                         633\n",
      "   Occupation\n",
      "1          22\n",
      "4         148\n",
      "2         200\n",
      "3         630\n",
      "   No of dependents\n",
      "2               155\n",
      "1               845\n",
      "   Telephone\n",
      "2        404\n",
      "1        596\n",
      "   Foreign Worker\n",
      "2              37\n",
      "1             963\n",
      "     y\n",
      "1  300\n",
      "0  700\n"
     ]
    }
   ],
   "source": [
    "for col in data.columns:\n",
    "    print(data[col].value_counts().sort_values().to_frame())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:47:13.378375Z",
     "start_time": "2019-05-29T07:47:13.348085Z"
    }
   },
   "outputs": [],
   "source": [
    "#计算变量分箱之后各分箱的坏样本率\n",
    "def BinBadRate(df, col, target, grantRateIndicator=0):\n",
    "    '''\n",
    "    :param df: 需要计算好坏比率的数据集\n",
    "    :param col: 需要计算好坏比率的特征\n",
    "    :param target: 好坏标签\n",
    "    :param grantRateIndicator: 1返回总体的坏样本率，0不返回\n",
    "    :return: 每箱的坏样本率，以及总体的坏样本率（当grantRateIndicator＝＝1时）\n",
    "    '''\n",
    "    #print(df.groupby([col])[target])\n",
    "    total = df.groupby([col])[target].count()\n",
    "    #print(total)\n",
    "    total = pd.DataFrame({'total': total})\n",
    "    #print(total)\n",
    "    bad = df.groupby([col])[target].sum()\n",
    "    bad = pd.DataFrame({'bad': bad})\n",
    "    #合并\n",
    "    regroup = total.merge(bad, left_index=True, right_index=True, how='left')\n",
    "    #print(regroup)\n",
    "    regroup.reset_index(level=0, inplace=True)\n",
    "    #print(regroup)\n",
    "    #计算坏样本率\n",
    "    regroup['bad_rate'] = regroup.apply(lambda x: x.bad * 1.0 / x.total, axis=1)\n",
    "    #print(regroup)\n",
    "    #生成字典，（变量名取值：坏样本率）\n",
    "    dicts = dict(zip(regroup[col],regroup['bad_rate']))\n",
    "    if grantRateIndicator==0:\n",
    "        return (dicts, regroup)\n",
    "    N = sum(regroup['total'])\n",
    "    B = sum(regroup['bad'])\n",
    "    #总体样本率\n",
    "    overallRate = B * 1.0 / N\n",
    "    return (dicts, regroup, overallRate)\n",
    "\n",
    "\n",
    "## 判断某变量的坏样本率是否单调\n",
    "def BadRateMonotone(df, sortByVar, target,special_attribute = []):\n",
    "    '''\n",
    "    :param df: 包含检验坏样本率的变量，和目标变量\n",
    "    :param sortByVar: 需要检验坏样本率的变量\n",
    "    :param target: 目标变量，0、1表示好、坏\n",
    "    :param special_attribute: 不参与检验的特殊值\n",
    "    :return: 坏样本率单调与否\n",
    "    '''\n",
    "    df2 = df.loc[~df[sortByVar].isin(special_attribute)]\n",
    "    if len(set(df2[sortByVar])) <= 2:\n",
    "        return True\n",
    "    regroup = BinBadRate(df2, sortByVar, target)[1]\n",
    "    combined = zip(regroup['total'],regroup['bad'])\n",
    "    badRate = [x[1]*1.0/x[0] for x in combined]\n",
    "    badRateNotMonotone = [badRate[i]<badRate[i+1] and badRate[i] < badRate[i-1] or badRate[i]>badRate[i+1] and badRate[i] > badRate[i-1]\n",
    "                          for i in range(1,len(badRate)-1)]\n",
    "    if True in badRateNotMonotone:\n",
    "        return False\n",
    "    else:\n",
    "        return True\n",
    "\n",
    "#计算WOE值\n",
    "def CalcWOE(df, col, target):\n",
    "    '''\n",
    "    :param df: 包含需要计算WOE的变量和目标变量\n",
    "    :param col: 需要计算WOE、IV的变量，必须是分箱后的变量，或者不需要分箱的类别型变量\n",
    "    :param target: 目标变量，0、1表示好、坏\n",
    "    :return: 返回WOE和IV\n",
    "    '''\n",
    "    total = df.groupby([col])[target].count()\n",
    "    total = pd.DataFrame({'total': total})\n",
    "    bad = df.groupby([col])[target].sum()\n",
    "    bad = pd.DataFrame({'bad': bad})\n",
    "    regroup = total.merge(bad, left_index=True, right_index=True, how='left')\n",
    "    regroup.reset_index(level=0, inplace=True)\n",
    "    N = sum(regroup['total'])\n",
    "    B = sum(regroup['bad'])\n",
    "    regroup['good'] = regroup['total'] - regroup['bad']\n",
    "    G = N - B\n",
    "    regroup['bad_pcnt'] = regroup['bad'].map(lambda x: x*1.0/B)\n",
    "    regroup['good_pcnt'] = regroup['good'].map(lambda x: x * 1.0 / G)\n",
    "    regroup['WOE'] = regroup.apply(lambda x: np.log(x.bad_pcnt*1.0/x.good_pcnt),axis = 1)\n",
    "    WOE_dict = regroup[[col,'WOE']].set_index(col).to_dict(orient='index')\n",
    "    for k, v in WOE_dict.items():\n",
    "        WOE_dict[k] = v['WOE']\n",
    "    IV = regroup.apply(lambda x: (x.good_pcnt-x.bad_pcnt)*np.log(x.good_pcnt*1.0/x.bad_pcnt),axis = 1)\n",
    "    IV = sum(IV)\n",
    "    return {\"WOE\": WOE_dict, 'IV':IV}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:47:13.519499Z",
     "start_time": "2019-05-29T07:47:13.380371Z"
    }
   },
   "outputs": [],
   "source": [
    "'''\n",
    "对类别型变量的分箱和WOE计算\n",
    "可以通过计算取值个数的方式判断是否是类别型变量\n",
    "'''\n",
    "#类别型变量\n",
    "categoricalFeatures = []\n",
    "#连续型变量\n",
    "numericalFeatures = []\n",
    "WOE_IV_dict = {}\n",
    "for var in allFeatures:\n",
    "    if len(set(data[var])) > 5:\n",
    "        numericalFeatures.append(var)\n",
    "    else:\n",
    "        categoricalFeatures.append(var)\n",
    "\n",
    "not_monotone =[]\n",
    "for var in categoricalFeatures:\n",
    "    #检查bad rate在箱中的单调性\n",
    "    if not BadRateMonotone(data, var, 'y'):\n",
    "        not_monotone.append(var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:47:13.588194Z",
     "start_time": "2019-05-29T07:47:13.522643Z"
    }
   },
   "outputs": [],
   "source": [
    "'''\n",
    "连续型变量\n",
    "卡方分箱\n",
    "'''\n",
    "def AssignBin(x, cutOffPoints,special_attribute=[]):\n",
    "    '''\n",
    "    :param x: 某个变量的某个取值\n",
    "    :param cutOffPoints: 上述变量的分箱结果，用切分点表示\n",
    "    :param special_attribute:  不参与分箱的特殊取值\n",
    "    :return: 分箱后的对应的第几个箱，从0开始\n",
    "    for example, if cutOffPoints = [10,20,30], if x = 7, return Bin 0. If x = 35, return Bin 3\n",
    "    '''\n",
    "    numBin = len(cutOffPoints) + 1 + len(special_attribute)\n",
    "    if x in special_attribute:\n",
    "        i = special_attribute.index(x)+1\n",
    "        return 'Bin {}'.format(0-i)\n",
    "    if x<=cutOffPoints[0]:\n",
    "        return 'Bin 0'\n",
    "    elif x > cutOffPoints[-1]:\n",
    "        return 'Bin {}'.format(numBin-1)\n",
    "    else:\n",
    "        for i in range(0,numBin-1):\n",
    "            if cutOffPoints[i] < x <=  cutOffPoints[i+1]:\n",
    "                return 'Bin {}'.format(i+1)\n",
    "\n",
    "\n",
    "\n",
    "def AssignGroup(x, bin):\n",
    "    '''\n",
    "    :param x: 某个变量的某个取值\n",
    "    :param bin: 上述变量的分箱结果\n",
    "    :return: x在分箱结果下的映射\n",
    "    '''\n",
    "    N = len(bin)\n",
    "    if x<=min(bin):\n",
    "        return min(bin)\n",
    "    elif x>max(bin):\n",
    "        return 10e10\n",
    "    else:\n",
    "        for i in range(N-1):\n",
    "            if bin[i] < x <= bin[i+1]:\n",
    "                return bin[i+1]\n",
    " \n",
    " \n",
    "def SplitData(df, col, numOfSplit, special_attribute=[]):\n",
    "    '''\n",
    "    :param df: 按照col排序后的数据集\n",
    "    :param col: 待分箱的变量\n",
    "    :param numOfSplit: 切分的组别数\n",
    "    :param special_attribute: 在切分数据集的时候，某些特殊值需要排除在外\n",
    "    :return: 在原数据集上增加一列，把原始细粒度的col重新划分成粗粒度的值，便于分箱中的合并处理\n",
    "    '''\n",
    "    df2 = df.copy()\n",
    "    if special_attribute != []:\n",
    "        df2 = df.loc[~df[col].isin(special_attribute)]\n",
    "    N = df2.shape[0]#行数\n",
    "    #\" / \"就表示 浮点数除法，返回浮点结果;\" // \"表示整数除法\n",
    "    n = N//numOfSplit #每组样本数\n",
    "    splitPointIndex = [i*n for i in range(1,numOfSplit)] #分割点的下标\n",
    "    '''\n",
    "    [i*2 for i in range(1,100)]\n",
    "    [2, 4, 6, 8, 10,......,198]\n",
    "    '''\n",
    "    rawValues = sorted(list(df2[col])) #对取值进行排序\n",
    "    #取到粗糙卡方划分节点\n",
    "    splitPoint = [rawValues[i] for i in splitPointIndex] #分割点的取值\n",
    "    splitPoint = sorted(list(set(splitPoint)))\n",
    "    return splitPoint\n",
    "\n",
    "\n",
    "#计算卡方值的函数\n",
    "def Chi2(df, total_col, bad_col, overallRate):\n",
    "    '''\n",
    "    :param df: 包含全部样本总计与坏样本总计的数据框\n",
    "    :param total_col: 全部样本的个数\n",
    "    :param bad_col: 坏样本的个数\n",
    "    :param overallRate: 全体样本的坏样本占比\n",
    "    :return: 卡方值\n",
    "    '''\n",
    "    df2 = df.copy()\n",
    "    # 期望坏样本个数＝全部样本个数*平均坏样本占比\n",
    "    df2['expected'] = df[total_col].apply(lambda x: x*overallRate)\n",
    "    combined = zip(df2['expected'], df2[bad_col])\n",
    "    chi = [(i[0]-i[1])**2/i[0] for i in combined]\n",
    "    chi2 = sum(chi)\n",
    "    return chi2\n",
    "\n",
    "\n",
    "##ChiMerge_MaxInterval：通过指定最大间隔数，使用卡方值分割连续变量\n",
    "def ChiMerge(df, col, target, max_interval=5,special_attribute=[],minBinPcnt=0):\n",
    "    '''\n",
    "    :param df: 包含目标变量与分箱属性的数据框\n",
    "    :param col: 需要分箱的属性\n",
    "    :param target: 目标变量，取值0或1\n",
    "    :param max_interval: 最大分箱数。如果原始属性的取值个数低于该参数，不执行这段函数\n",
    "    :param special_attribute: 不参与分箱的属性取值，缺失值的情况\n",
    "    :param minBinPcnt：最小箱的占比，默认为0\n",
    "    :return: 分箱结果\n",
    "    '''\n",
    "    colLevels = sorted(list(set(df[col])))\n",
    "    N_distinct = len(colLevels)#不同的取值个数\n",
    "    if N_distinct <= max_interval:  #如果原始属性的取值个数低于max_interval，不执行这段函数\n",
    "        print (\"原始属性{}的取值个数低于max_interval\".format(col))\n",
    "        #分箱分数间隔段，少一个值也可以\n",
    "        #返回值colLevels会少一个最大值\n",
    "        return colLevels[:-1]\n",
    "    else:\n",
    "        if len(special_attribute)>=1:\n",
    "            #df1数据框取data_01中col那一列为特殊值的数据集\n",
    "            #df1 = df.loc[df[col].isin(special_attribute)]\n",
    "            print('{} 有缺失值的情况'.format(col))\n",
    "            #用逆函数对筛选后的结果取余，起删除指定行作用\n",
    "            df2 = df.loc[~df[col].isin(special_attribute)]\n",
    "        else:\n",
    "            df2 = df.copy()\n",
    "        N_distinct = len(list(set(df2[col])))#该特征不同的取值\n",
    " \n",
    "        # 步骤一: 通过col对数据集进行分组，求出每组的总样本数与坏样本数\n",
    "        if N_distinct > 100:\n",
    "            '''\n",
    "            split_x样例\n",
    "            [2, 8, 9.3, 10, 30,......,1800]\n",
    "            '''\n",
    "            split_x = SplitData(df2, col, 100)\n",
    "            #把值变为划分点的值\n",
    "            df2['temp'] = df2[col].map(lambda x: AssignGroup(x, split_x))\n",
    "        else:\n",
    "            #假如数值取值小于100就不发生变化了\n",
    "            df2['temp'] = df2[col]\n",
    "        # 总体bad rate将被用来计算expected bad count\n",
    "        (binBadRate, regroup, overallRate) = BinBadRate(df2, 'temp', target, grantRateIndicator=1)\n",
    " \n",
    "        # 首先，每个单独的属性值将被分为单独的一组\n",
    "        # 对属性值进行排序，然后两两组别进行合并\n",
    "        colLevels = sorted(list(set(df2['temp'])))\n",
    "        groupIntervals = [[i] for i in colLevels]\n",
    " \n",
    "        # 步骤二：建立循环，不断合并最优的相邻两个组别，直到：\n",
    "        # 1，最终分裂出来的分箱数<＝预设的最大分箱数\n",
    "        # 2，每箱的占比不低于预设值（可选）\n",
    "        # 3，每箱同时包含好坏样本\n",
    "        # 如果有特殊属性，那么最终分裂出来的分箱数＝预设的最大分箱数－特殊属性的个数\n",
    "        split_intervals = max_interval - len(special_attribute)\n",
    "        while (len(groupIntervals) > split_intervals):  # 终止条件: 当前分箱数＝预设的分箱数\n",
    "            # 每次循环时, 计算合并相邻组别后的卡方值。具有最小卡方值的合并方案，是最优方案\n",
    "            #存储卡方值\n",
    "            chisqList = []\n",
    "            for k in range(len(groupIntervals)-1):\n",
    "                temp_group = groupIntervals[k] + groupIntervals[k+1]\n",
    "                df2b = regroup.loc[regroup['temp'].isin(temp_group)]\n",
    "                chisq = Chi2(df2b, 'total', 'bad', overallRate)\n",
    "                chisqList.append(chisq)\n",
    "            best_comnbined = chisqList.index(min(chisqList))\n",
    "            groupIntervals[best_comnbined] = groupIntervals[best_comnbined] + groupIntervals[best_comnbined+1]\n",
    "            # after combining two intervals, we need to remove one of them\n",
    "            groupIntervals.remove(groupIntervals[best_comnbined+1])\n",
    "        groupIntervals = [sorted(i) for i in groupIntervals]\n",
    "        cutOffPoints = [max(i) for i in groupIntervals[:-1]]\n",
    " \n",
    "        # 检查是否有箱没有好或者坏样本。如果有，需要跟相邻的箱进行合并，直到每箱同时包含好坏样本\n",
    "        groupedvalues = df2['temp'].apply(lambda x: AssignBin(x, cutOffPoints))\n",
    "        #已成完成卡方分箱，但是没有考虑其单调性\n",
    "        df2['temp_Bin'] = groupedvalues\n",
    "        (binBadRate,regroup) = BinBadRate(df2, 'temp_Bin', target)\n",
    "        [minBadRate, maxBadRate] = [min(binBadRate.values()),max(binBadRate.values())]\n",
    "        while minBadRate ==0 or maxBadRate == 1:\n",
    "            # 找出全部为好／坏样本的箱\n",
    "            indexForBad01 = regroup[regroup['bad_rate'].isin([0,1])].temp_Bin.tolist()\n",
    "            bin=indexForBad01[0]\n",
    "            # 如果是最后一箱，则需要和上一个箱进行合并，也就意味着分裂点cutOffPoints中的最后一个需要移除\n",
    "            if bin == max(regroup.temp_Bin):\n",
    "                cutOffPoints = cutOffPoints[:-1]\n",
    "            # 如果是第一箱，则需要和下一个箱进行合并，也就意味着分裂点cutOffPoints中的第一个需要移除\n",
    "            elif bin == min(regroup.temp_Bin):\n",
    "                cutOffPoints = cutOffPoints[1:]\n",
    "            # 如果是中间的某一箱，则需要和前后中的一个箱进行合并，依据是较小的卡方值\n",
    "            else:\n",
    "                # 和前一箱进行合并，并且计算卡方值\n",
    "                currentIndex = list(regroup.temp_Bin).index(bin)\n",
    "                prevIndex = list(regroup.temp_Bin)[currentIndex - 1]\n",
    "                df3 = df2.loc[df2['temp_Bin'].isin([prevIndex, bin])]\n",
    "                (binBadRate, df2b) = BinBadRate(df3, 'temp_Bin', target)\n",
    "                chisq1 = Chi2(df2b, 'total', 'bad', overallRate)\n",
    "                # 和后一箱进行合并，并且计算卡方值\n",
    "                laterIndex = list(regroup.temp_Bin)[currentIndex + 1]\n",
    "                df3b = df2.loc[df2['temp_Bin'].isin([laterIndex, bin])]\n",
    "                (binBadRate, df2b) = BinBadRate(df3b, 'temp_Bin', target)\n",
    "                chisq2 = Chi2(df2b, 'total', 'bad', overallRate)\n",
    "                if chisq1 < chisq2:\n",
    "                    cutOffPoints.remove(cutOffPoints[currentIndex - 1])\n",
    "                else:\n",
    "                    cutOffPoints.remove(cutOffPoints[currentIndex])\n",
    "            # 完成合并之后，需要再次计算新的分箱准则下，每箱是否同时包含好坏样本\n",
    "            groupedvalues = df2['temp'].apply(lambda x: AssignBin(x, cutOffPoints))\n",
    "            df2['temp_Bin'] = groupedvalues\n",
    "            (binBadRate, regroup) = BinBadRate(df2, 'temp_Bin', target)\n",
    "            [minBadRate, maxBadRate] = [min(binBadRate.values()), max(binBadRate.values())]\n",
    " \n",
    "        # 需要检查分箱后的最小占比\n",
    "        if minBinPcnt > 0:\n",
    "            groupedvalues = df2['temp'].apply(lambda x: AssignBin(x, cutOffPoints))\n",
    "            df2['temp_Bin'] = groupedvalues\n",
    "            #value_counts每个数值出现了多少次\n",
    "            valueCounts = groupedvalues.value_counts().to_frame()\n",
    "            N=sum(valueCounts['temp'])\n",
    "            valueCounts['pcnt'] = valueCounts['temp'].apply(lambda x: x * 1.0 / N)\n",
    "            valueCounts = valueCounts.sort_index()\n",
    "            minPcnt = min(valueCounts['pcnt'])\n",
    "            #一定要箱数大于2才可以，要不就不能再合并了\n",
    "            while minPcnt < minBinPcnt and len(cutOffPoints) > 2:\n",
    "                # 找出占比最小的箱\n",
    "                indexForMinPcnt = valueCounts[valueCounts['pcnt'] == minPcnt].index.tolist()[0]\n",
    "                # 如果占比最小的箱是最后一箱，则需要和上一个箱进行合并，也就意味着分裂点cutOffPoints中的最后一个需要移除\n",
    "                if indexForMinPcnt == max(valueCounts.index):\n",
    "                    cutOffPoints = cutOffPoints[:-1]\n",
    "                # 如果占比最小的箱是第一箱，则需要和下一个箱进行合并，也就意味着分裂点cutOffPoints中的第一个需要移除\n",
    "                elif indexForMinPcnt == min(valueCounts.index):\n",
    "                    cutOffPoints = cutOffPoints[1:]\n",
    "                # 如果占比最小的箱是中间的某一箱，则需要和前后中的一个箱进行合并，依据是较小的卡方值\n",
    "                else:\n",
    "                    # 和前一箱进行合并，并且计算卡方值\n",
    "                    currentIndex = list(valueCounts.index).index(indexForMinPcnt)\n",
    "                    prevIndex = list(valueCounts.index)[currentIndex - 1]\n",
    "                    df3 = df2.loc[df2['temp_Bin'].isin([prevIndex, indexForMinPcnt])]\n",
    "                    (binBadRate, df2b) = BinBadRate(df3, 'temp_Bin', target)\n",
    "                    chisq1 = Chi2(df2b, 'total', 'bad', overallRate)\n",
    "                    # 和后一箱进行合并，并且计算卡方值\n",
    "                    laterIndex = list(valueCounts.index)[currentIndex + 1]\n",
    "                    df3b = df2.loc[df2['temp_Bin'].isin([laterIndex, indexForMinPcnt])]\n",
    "                    (binBadRate, df2b) = BinBadRate(df3b, 'temp_Bin', target)\n",
    "                    chisq2 = Chi2(df2b, 'total', 'bad', overallRate)\n",
    "                    if chisq1 < chisq2:\n",
    "                        cutOffPoints.remove(cutOffPoints[currentIndex - 1])\n",
    "                    else:\n",
    "                        cutOffPoints.remove(cutOffPoints[currentIndex])\n",
    "        cutOffPoints = special_attribute + cutOffPoints\n",
    "        return cutOffPoints\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.321379Z",
     "start_time": "2019-05-29T07:47:13.590885Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********* 分箱结果（分割点）************\n",
      "[{'Duration of Credit (month)': [6, 15]}, {'Purpose': [1]}, {'Credit Amount': [2101.0]}, {'Age (years)': [25, 36]}]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "对于数值型变量，需要先分箱，再计算WOE、IV\n",
    "分箱的结果需要满足：\n",
    "1，箱数不超过5\n",
    "2，bad rate单调\n",
    "3，每箱占比不低于5%\n",
    "'''\n",
    "binNums = {'Age':5, 'TaCA':3}\n",
    "bin_dict = []\n",
    "for var in numericalFeatures:\n",
    "    binNum = 5 # binNums[var]+1\n",
    "    newBin = var + '_Bin'\n",
    "    bin = ChiMerge(data, var, 'y',max_interval=binNum,minBinPcnt = 0.05)\n",
    "    data[newBin] = data[var].apply(lambda x: AssignBin(x,bin))\n",
    "    # 如果不满足单调性，就降低分箱个数\n",
    "    while not BadRateMonotone(data, newBin, 'y'):\n",
    "        binNum -= 1\n",
    "        bin = ChiMerge(data, var, 'y', max_interval=binNum, minBinPcnt=0.05)\n",
    "        data[newBin] = data[var].apply(lambda x: AssignBin(x, bin))\n",
    "    WOE_IV_dict[newBin] = CalcWOE(data, newBin, 'y')\n",
    "    bin_dict.append({var:bin})\n",
    "print('********* 分箱结果（分割点）************')\n",
    "print(bin_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.428567Z",
     "start_time": "2019-05-29T07:48:02.323507Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Account Balance</th>\n",
       "      <th>Duration of Credit (month)</th>\n",
       "      <th>Payment Status of Previous Credit</th>\n",
       "      <th>Purpose</th>\n",
       "      <th>Credit Amount</th>\n",
       "      <th>Value Savings/Stocks</th>\n",
       "      <th>Length of current employment</th>\n",
       "      <th>Instalment per cent</th>\n",
       "      <th>Sex &amp; Marital Status</th>\n",
       "      <th>Guarantors</th>\n",
       "      <th>Duration in Current address</th>\n",
       "      <th>Most valuable available asset</th>\n",
       "      <th>Age (years)</th>\n",
       "      <th>Concurrent Credits</th>\n",
       "      <th>Type of apartment</th>\n",
       "      <th>No of Credits at this Bank</th>\n",
       "      <th>Occupation</th>\n",
       "      <th>No of dependents</th>\n",
       "      <th>Telephone</th>\n",
       "      <th>Foreign Worker</th>\n",
       "      <th>y</th>\n",
       "      <th>Duration of Credit (month)_Bin</th>\n",
       "      <th>Purpose_Bin</th>\n",
       "      <th>Credit Amount_Bin</th>\n",
       "      <th>Age (years)_Bin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1049</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>21</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2799</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>841</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2122</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2171</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2241</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>48</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3398</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1361</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>4</td>\n",
       "      <td>18</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1098</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>65</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3758</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3905</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>6187</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1957</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2</td>\n",
       "      <td>48</td>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "      <td>7582</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>31</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1936</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2647</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3939</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3213</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>25</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2</td>\n",
       "      <td>36</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2337</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>4</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>7228</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>39</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3676</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>37</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3124</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>49</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2</td>\n",
       "      <td>36</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2384</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>33</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1424</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>26</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4716</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4771</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>51</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>652</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>24</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1154</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>37</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>4</td>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3556</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>29</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>3</td>\n",
       "      <td>42</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4796</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>56</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>970</th>\n",
       "      <td>4</td>\n",
       "      <td>48</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>4844</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>33</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>971</th>\n",
       "      <td>2</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>8318</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>42</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>972</th>\n",
       "      <td>3</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2100</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>37</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>973</th>\n",
       "      <td>1</td>\n",
       "      <td>45</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>11816</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>29</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>974</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>448</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>975</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>11998</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>34</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>976</th>\n",
       "      <td>2</td>\n",
       "      <td>48</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>18424</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>977</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>14896</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>68</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>978</th>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2762</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>25</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>979</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3386</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>35</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>980</th>\n",
       "      <td>2</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2039</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>981</th>\n",
       "      <td>4</td>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "      <td>9</td>\n",
       "      <td>2169</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>28</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>982</th>\n",
       "      <td>2</td>\n",
       "      <td>48</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5096</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>983</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1882</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>25</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>984</th>\n",
       "      <td>1</td>\n",
       "      <td>48</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6999</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>34</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>985</th>\n",
       "      <td>4</td>\n",
       "      <td>12</td>\n",
       "      <td>4</td>\n",
       "      <td>9</td>\n",
       "      <td>2292</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>42</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>986</th>\n",
       "      <td>1</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>8978</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>987</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>674</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>988</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>976</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>989</th>\n",
       "      <td>2</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2718</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>990</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>750</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>27</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>991</th>\n",
       "      <td>2</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>12579</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>44</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>992</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>7511</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>51</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>993</th>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3966</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>994</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>6199</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>1</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1987</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>21</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount1_[-inf, 2101.0)</td>\n",
       "      <td>Age (years)1_[-inf, 25)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>1</td>\n",
       "      <td>24</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2303</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>4</td>\n",
       "      <td>21</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>12680</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose1_[-inf, 1)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6468</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>52</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)2_[6, 15)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)3_[36, inf)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6350</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>31</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Duration of Credit (month)3_[15, inf)</td>\n",
       "      <td>Purpose2_[1, inf)</td>\n",
       "      <td>Credit Amount2_[2101.0, inf)</td>\n",
       "      <td>Age (years)2_[25, 36)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Account Balance  Duration of Credit (month)  \\\n",
       "0                  1                          18   \n",
       "1                  1                           9   \n",
       "2                  2                          12   \n",
       "3                  1                          12   \n",
       "4                  1                          12   \n",
       "5                  1                          10   \n",
       "6                  1                           8   \n",
       "7                  1                           6   \n",
       "8                  4                          18   \n",
       "9                  2                          24   \n",
       "10                 1                          11   \n",
       "11                 1                          30   \n",
       "12                 1                           6   \n",
       "13                 2                          48   \n",
       "14                 1                          18   \n",
       "15                 1                           6   \n",
       "16                 1                          11   \n",
       "17                 2                          18   \n",
       "18                 2                          36   \n",
       "19                 4                          11   \n",
       "20                 1                           6   \n",
       "21                 2                          12   \n",
       "22                 2                          36   \n",
       "23                 2                          12   \n",
       "24                 1                           6   \n",
       "25                 2                          11   \n",
       "26                 1                          12   \n",
       "27                 2                           9   \n",
       "28                 4                          15   \n",
       "29                 3                          42   \n",
       "..               ...                         ...   \n",
       "970                4                          48   \n",
       "971                2                          27   \n",
       "972                3                          18   \n",
       "973                1                          45   \n",
       "974                1                           6   \n",
       "975                1                          30   \n",
       "976                2                          48   \n",
       "977                1                           6   \n",
       "978                2                          12   \n",
       "979                1                          12   \n",
       "980                2                          24   \n",
       "981                4                          18   \n",
       "982                2                          48   \n",
       "983                1                          18   \n",
       "984                1                          48   \n",
       "985                4                          12   \n",
       "986                1                          14   \n",
       "987                1                          12   \n",
       "988                1                          18   \n",
       "989                2                          24   \n",
       "990                1                          18   \n",
       "991                2                          24   \n",
       "992                1                          18   \n",
       "993                1                          18   \n",
       "994                1                          12   \n",
       "995                1                          24   \n",
       "996                1                          24   \n",
       "997                4                          21   \n",
       "998                2                          12   \n",
       "999                1                          30   \n",
       "\n",
       "     Payment Status of Previous Credit  Purpose  Credit Amount  \\\n",
       "0                                    4        2           1049   \n",
       "1                                    4        0           2799   \n",
       "2                                    2        9            841   \n",
       "3                                    4        0           2122   \n",
       "4                                    4        0           2171   \n",
       "5                                    4        0           2241   \n",
       "6                                    4        0           3398   \n",
       "7                                    4        0           1361   \n",
       "8                                    4        3           1098   \n",
       "9                                    2        3           3758   \n",
       "10                                   4        0           3905   \n",
       "11                                   4        1           6187   \n",
       "12                                   4        3           1957   \n",
       "13                                   3       10           7582   \n",
       "14                                   2        3           1936   \n",
       "15                                   2        3           2647   \n",
       "16                                   4        0           3939   \n",
       "17                                   2        3           3213   \n",
       "18                                   4        3           2337   \n",
       "19                                   4        0           7228   \n",
       "20                                   4        0           3676   \n",
       "21                                   4        0           3124   \n",
       "22                                   2        5           2384   \n",
       "23                                   4        4           1424   \n",
       "24                                   4        0           4716   \n",
       "25                                   3        3           4771   \n",
       "26                                   2        2            652   \n",
       "27                                   4        3           1154   \n",
       "28                                   2        0           3556   \n",
       "29                                   4        1           4796   \n",
       "..                                 ...      ...            ...   \n",
       "970                                  2        9           4844   \n",
       "971                                  0        9           8318   \n",
       "972                                  2        3           2100   \n",
       "973                                  0        9          11816   \n",
       "974                                  2        6            448   \n",
       "975                                  2        5          11998   \n",
       "976                                  0       10          18424   \n",
       "977                                  2        0          14896   \n",
       "978                                  2        2           2762   \n",
       "979                                  2        1           3386   \n",
       "980                                  2        3           2039   \n",
       "981                                  3        9           2169   \n",
       "982                                  4        2           5096   \n",
       "983                                  2        3           1882   \n",
       "984                                  2        3           6999   \n",
       "985                                  4        9           2292   \n",
       "986                                  2        0           8978   \n",
       "987                                  2        3            674   \n",
       "988                                  2        0            976   \n",
       "989                                  2        0           2718   \n",
       "990                                  2        6            750   \n",
       "991                                  2        1          12579   \n",
       "992                                  2        1           7511   \n",
       "993                                  4        0           3966   \n",
       "994                                  0        3           6199   \n",
       "995                                  2        3           1987   \n",
       "996                                  2        0           2303   \n",
       "997                                  4        0          12680   \n",
       "998                                  2        3           6468   \n",
       "999                                  2        2           6350   \n",
       "\n",
       "     Value Savings/Stocks  Length of current employment  Instalment per cent  \\\n",
       "0                       1                             2                    4   \n",
       "1                       1                             3                    2   \n",
       "2                       2                             4                    2   \n",
       "3                       1                             3                    3   \n",
       "4                       1                             3                    4   \n",
       "5                       1                             2                    1   \n",
       "6                       1                             4                    1   \n",
       "7                       1                             2                    2   \n",
       "8                       1                             1                    4   \n",
       "9                       3                             1                    1   \n",
       "10                      1                             3                    2   \n",
       "11                      2                             4                    1   \n",
       "12                      1                             4                    1   \n",
       "13                      2                             1                    2   \n",
       "14                      5                             4                    2   \n",
       "15                      3                             3                    2   \n",
       "16                      1                             3                    1   \n",
       "17                      3                             2                    1   \n",
       "18                      1                             5                    4   \n",
       "19                      1                             3                    1   \n",
       "20                      1                             3                    1   \n",
       "21                      1                             2                    1   \n",
       "22                      1                             2                    4   \n",
       "23                      1                             4                    4   \n",
       "24                      5                             2                    1   \n",
       "25                      1                             4                    2   \n",
       "26                      1                             5                    4   \n",
       "27                      1                             5                    2   \n",
       "28                      5                             3                    3   \n",
       "29                      1                             5                    4   \n",
       "..                    ...                           ...                  ...   \n",
       "970                     1                             1                    3   \n",
       "971                     1                             5                    2   \n",
       "972                     1                             3                    4   \n",
       "973                     1                             5                    2   \n",
       "974                     1                             2                    4   \n",
       "975                     1                             2                    1   \n",
       "976                     1                             3                    1   \n",
       "977                     1                             5                    1   \n",
       "978                     5                             5                    1   \n",
       "979                     1                             5                    3   \n",
       "980                     1                             2                    1   \n",
       "981                     1                             3                    4   \n",
       "982                     1                             3                    2   \n",
       "983                     1                             3                    4   \n",
       "984                     1                             4                    1   \n",
       "985                     1                             1                    4   \n",
       "986                     1                             5                    1   \n",
       "987                     2                             4                    4   \n",
       "988                     1                             2                    1   \n",
       "989                     1                             3                    3   \n",
       "990                     1                             1                    4   \n",
       "991                     1                             5                    4   \n",
       "992                     5                             5                    1   \n",
       "993                     1                             5                    1   \n",
       "994                     1                             3                    4   \n",
       "995                     1                             3                    2   \n",
       "996                     1                             5                    4   \n",
       "997                     5                             5                    4   \n",
       "998                     5                             1                    2   \n",
       "999                     5                             5                    4   \n",
       "\n",
       "     Sex & Marital Status  Guarantors  Duration in Current address  \\\n",
       "0                       2           1                            4   \n",
       "1                       3           1                            2   \n",
       "2                       2           1                            4   \n",
       "3                       3           1                            2   \n",
       "4                       3           1                            4   \n",
       "5                       3           1                            3   \n",
       "6                       3           1                            4   \n",
       "7                       3           1                            4   \n",
       "8                       2           1                            4   \n",
       "9                       2           1                            4   \n",
       "10                      3           1                            2   \n",
       "11                      4           1                            4   \n",
       "12                      2           1                            4   \n",
       "13                      3           1                            4   \n",
       "14                      4           1                            4   \n",
       "15                      3           1                            3   \n",
       "16                      3           1                            2   \n",
       "17                      4           1                            3   \n",
       "18                      3           1                            4   \n",
       "19                      3           1                            4   \n",
       "20                      3           1                            3   \n",
       "21                      3           1                            3   \n",
       "22                      3           1                            1   \n",
       "23                      3           1                            3   \n",
       "24                      3           1                            3   \n",
       "25                      3           1                            4   \n",
       "26                      2           1                            4   \n",
       "27                      3           1                            4   \n",
       "28                      3           1                            2   \n",
       "29                      3           1                            4   \n",
       "..                    ...         ...                          ...   \n",
       "970                     3           1                            2   \n",
       "971                     2           1                            4   \n",
       "972                     3           2                            2   \n",
       "973                     3           1                            4   \n",
       "974                     2           1                            4   \n",
       "975                     1           1                            1   \n",
       "976                     2           1                            2   \n",
       "977                     3           1                            4   \n",
       "978                     2           1                            2   \n",
       "979                     3           1                            4   \n",
       "980                     4           1                            1   \n",
       "981                     4           1                            2   \n",
       "982                     2           1                            3   \n",
       "983                     2           1                            4   \n",
       "984                     4           3                            1   \n",
       "985                     3           1                            2   \n",
       "986                     1           1                            4   \n",
       "987                     4           1                            1   \n",
       "988                     2           1                            2   \n",
       "989                     2           1                            4   \n",
       "990                     2           1                            1   \n",
       "991                     2           1                            2   \n",
       "992                     3           1                            4   \n",
       "993                     2           1                            4   \n",
       "994                     3           1                            2   \n",
       "995                     3           1                            4   \n",
       "996                     3           2                            1   \n",
       "997                     3           1                            4   \n",
       "998                     3           1                            1   \n",
       "999                     3           1                            4   \n",
       "\n",
       "     Most valuable available asset  Age (years)  Concurrent Credits  \\\n",
       "0                                2           21                   3   \n",
       "1                                1           36                   3   \n",
       "2                                1           23                   3   \n",
       "3                                1           39                   3   \n",
       "4                                2           38                   1   \n",
       "5                                1           48                   3   \n",
       "6                                1           39                   3   \n",
       "7                                1           40                   3   \n",
       "8                                3           65                   3   \n",
       "9                                4           23                   3   \n",
       "10                               1           36                   3   \n",
       "11                               3           24                   3   \n",
       "12                               3           31                   3   \n",
       "13                               4           31                   3   \n",
       "14                               3           23                   3   \n",
       "15                               1           44                   3   \n",
       "16                               1           40                   3   \n",
       "17                               1           25                   3   \n",
       "18                               1           36                   3   \n",
       "19                               2           39                   3   \n",
       "20                               1           37                   3   \n",
       "21                               1           49                   1   \n",
       "22                               4           33                   3   \n",
       "23                               2           26                   3   \n",
       "24                               1           44                   3   \n",
       "25                               2           51                   3   \n",
       "26                               2           24                   3   \n",
       "27                               1           37                   3   \n",
       "28                               4           29                   3   \n",
       "29                               4           56                   3   \n",
       "..                             ...          ...                 ...   \n",
       "970                              3           33                   3   \n",
       "971                              4           42                   2   \n",
       "972                              1           37                   3   \n",
       "973                              3           29                   3   \n",
       "974                              2           23                   3   \n",
       "975                              4           34                   1   \n",
       "976                              2           32                   1   \n",
       "977                              4           68                   1   \n",
       "978                              2           25                   3   \n",
       "979                              4           35                   3   \n",
       "980                              2           22                   3   \n",
       "981                              3           28                   3   \n",
       "982                              3           30                   3   \n",
       "983                              3           25                   1   \n",
       "984                              1           34                   3   \n",
       "985                              3           42                   2   \n",
       "986                              2           45                   3   \n",
       "987                              2           20                   3   \n",
       "988                              3           23                   3   \n",
       "989                              2           20                   3   \n",
       "990                              1           27                   3   \n",
       "991                              4           44                   3   \n",
       "992                              2           51                   3   \n",
       "993                              1           33                   1   \n",
       "994                              2           28                   3   \n",
       "995                              1           21                   3   \n",
       "996                              1           45                   3   \n",
       "997                              4           30                   3   \n",
       "998                              4           52                   3   \n",
       "999                              2           31                   3   \n",
       "\n",
       "     Type of apartment  No of Credits at this Bank  Occupation  \\\n",
       "0                    1                           1           3   \n",
       "1                    1                           2           3   \n",
       "2                    1                           1           2   \n",
       "3                    1                           2           2   \n",
       "4                    2                           2           2   \n",
       "5                    1                           2           2   \n",
       "6                    2                           2           2   \n",
       "7                    2                           1           2   \n",
       "8                    2                           2           1   \n",
       "9                    1                           1           1   \n",
       "10                   1                           2           3   \n",
       "11                   1                           2           3   \n",
       "12                   2                           1           3   \n",
       "13                   2                           1           4   \n",
       "14                   1                           2           2   \n",
       "15                   1                           1           3   \n",
       "16                   2                           2           2   \n",
       "17                   1                           1           3   \n",
       "18                   2                           1           3   \n",
       "19                   2                           2           2   \n",
       "20                   1                           3           3   \n",
       "21                   2                           2           2   \n",
       "22                   1                           1           2   \n",
       "23                   2                           1           3   \n",
       "24                   2                           2           2   \n",
       "25                   2                           1           3   \n",
       "26                   1                           1           3   \n",
       "27                   2                           3           2   \n",
       "28                   2                           1           3   \n",
       "29                   3                           1           3   \n",
       "..                 ...                         ...         ...   \n",
       "970                  1                           1           4   \n",
       "971                  3                           2           4   \n",
       "972                  2                           1           3   \n",
       "973                  1                           2           3   \n",
       "974                  2                           1           3   \n",
       "975                  2                           1           2   \n",
       "976                  2                           1           4   \n",
       "977                  2                           1           4   \n",
       "978                  2                           1           3   \n",
       "979                  3                           1           3   \n",
       "980                  2                           1           3   \n",
       "981                  2                           1           3   \n",
       "982                  2                           1           4   \n",
       "983                  1                           2           3   \n",
       "984                  2                           2           3   \n",
       "985                  2                           2           4   \n",
       "986                  2                           1           4   \n",
       "987                  2                           1           3   \n",
       "988                  2                           1           2   \n",
       "989                  1                           1           2   \n",
       "990                  2                           1           1   \n",
       "991                  3                           1           4   \n",
       "992                  3                           1           3   \n",
       "993                  1                           3           3   \n",
       "994                  1                           2           3   \n",
       "995                  1                           1           2   \n",
       "996                  2                           1           3   \n",
       "997                  3                           1           4   \n",
       "998                  2                           1           4   \n",
       "999                  2                           1           3   \n",
       "\n",
       "     No of dependents  Telephone  Foreign Worker  y  \\\n",
       "0                   1          1               1  0   \n",
       "1                   2          1               1  0   \n",
       "2                   1          1               1  0   \n",
       "3                   2          1               2  0   \n",
       "4                   1          1               2  0   \n",
       "5                   2          1               2  0   \n",
       "6                   1          1               2  0   \n",
       "7                   2          1               2  0   \n",
       "8                   1          1               1  0   \n",
       "9                   1          1               1  0   \n",
       "10                  2          1               1  0   \n",
       "11                  1          1               1  0   \n",
       "12                  1          1               1  0   \n",
       "13                  1          2               1  0   \n",
       "14                  1          1               1  0   \n",
       "15                  2          1               1  0   \n",
       "16                  2          1               1  0   \n",
       "17                  1          1               1  0   \n",
       "18                  1          1               1  0   \n",
       "19                  1          1               1  0   \n",
       "20                  2          1               1  0   \n",
       "21                  2          1               1  0   \n",
       "22                  1          1               1  1   \n",
       "23                  1          1               1  0   \n",
       "24                  2          1               1  0   \n",
       "25                  1          1               1  0   \n",
       "26                  1          1               1  0   \n",
       "27                  1          1               1  0   \n",
       "28                  1          1               1  0   \n",
       "29                  1          1               1  0   \n",
       "..                ...        ...             ... ..   \n",
       "970                 1          2               1  1   \n",
       "971                 1          2               1  1   \n",
       "972                 1          1               1  1   \n",
       "973                 1          1               1  1   \n",
       "974                 1          1               1  1   \n",
       "975                 1          2               1  1   \n",
       "976                 1          2               2  1   \n",
       "977                 1          2               1  1   \n",
       "978                 1          2               1  1   \n",
       "979                 1          2               1  1   \n",
       "980                 1          2               1  1   \n",
       "981                 1          2               1  1   \n",
       "982                 1          2               1  1   \n",
       "983                 1          1               1  1   \n",
       "984                 1          2               1  1   \n",
       "985                 1          2               1  1   \n",
       "986                 1          2               2  1   \n",
       "987                 1          1               1  1   \n",
       "988                 1          1               1  1   \n",
       "989                 1          2               1  1   \n",
       "990                 1          1               1  1   \n",
       "991                 1          2               1  1   \n",
       "992                 2          2               1  1   \n",
       "993                 1          2               1  1   \n",
       "994                 1          2               1  1   \n",
       "995                 2          1               1  1   \n",
       "996                 1          1               1  1   \n",
       "997                 1          2               1  1   \n",
       "998                 1          2               1  1   \n",
       "999                 1          1               1  1   \n",
       "\n",
       "            Duration of Credit (month)_Bin         Purpose_Bin  \\\n",
       "0    Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "1      Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "2      Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "3      Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "4      Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "5      Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "6      Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "7      Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "8    Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "9    Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "10     Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "11   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "12     Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "13   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "14   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "15     Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "16     Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "17   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "18   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "19     Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "20     Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "21     Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "22   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "23     Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "24     Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "25     Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "26     Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "27     Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "28   Duration of Credit (month)3_[15, inf)  Purpose1_[-inf, 1)   \n",
       "29   Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "..                                     ...                 ...   \n",
       "970  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "971  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "972  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "973  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "974    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "975  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "976  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "977    Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "978    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "979    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "980  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "981  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "982  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "983  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "984  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "985    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "986    Duration of Credit (month)2_[6, 15)  Purpose1_[-inf, 1)   \n",
       "987    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "988  Duration of Credit (month)3_[15, inf)  Purpose1_[-inf, 1)   \n",
       "989  Duration of Credit (month)3_[15, inf)  Purpose1_[-inf, 1)   \n",
       "990  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "991  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "992  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "993  Duration of Credit (month)3_[15, inf)  Purpose1_[-inf, 1)   \n",
       "994    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "995  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "996  Duration of Credit (month)3_[15, inf)  Purpose1_[-inf, 1)   \n",
       "997  Duration of Credit (month)3_[15, inf)  Purpose1_[-inf, 1)   \n",
       "998    Duration of Credit (month)2_[6, 15)   Purpose2_[1, inf)   \n",
       "999  Duration of Credit (month)3_[15, inf)   Purpose2_[1, inf)   \n",
       "\n",
       "                 Credit Amount_Bin          Age (years)_Bin  \n",
       "0    Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "1     Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "2    Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "3     Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "4     Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "5     Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "6     Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "7    Credit Amount1_[-inf, 2101.0)   Age (years)3_[36, inf)  \n",
       "8    Credit Amount1_[-inf, 2101.0)   Age (years)3_[36, inf)  \n",
       "9     Credit Amount2_[2101.0, inf)  Age (years)1_[-inf, 25)  \n",
       "10    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "11    Credit Amount2_[2101.0, inf)  Age (years)1_[-inf, 25)  \n",
       "12   Credit Amount1_[-inf, 2101.0)    Age (years)2_[25, 36)  \n",
       "13    Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "14   Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "15    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "16    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "17    Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "18    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "19    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "20    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "21    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "22    Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "23   Credit Amount1_[-inf, 2101.0)    Age (years)2_[25, 36)  \n",
       "24    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "25    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "26   Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "27   Credit Amount1_[-inf, 2101.0)   Age (years)3_[36, inf)  \n",
       "28    Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "29    Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "..                             ...                      ...  \n",
       "970   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "971   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "972  Credit Amount1_[-inf, 2101.0)   Age (years)3_[36, inf)  \n",
       "973   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "974  Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "975   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "976   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "977   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "978   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "979   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "980  Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "981   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "982   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "983  Credit Amount1_[-inf, 2101.0)    Age (years)2_[25, 36)  \n",
       "984   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "985   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "986   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "987  Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "988  Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "989   Credit Amount2_[2101.0, inf)  Age (years)1_[-inf, 25)  \n",
       "990  Credit Amount1_[-inf, 2101.0)    Age (years)2_[25, 36)  \n",
       "991   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "992   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "993   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "994   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "995  Credit Amount1_[-inf, 2101.0)  Age (years)1_[-inf, 25)  \n",
       "996   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "997   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "998   Credit Amount2_[2101.0, inf)   Age (years)3_[36, inf)  \n",
       "999   Credit Amount2_[2101.0, inf)    Age (years)2_[25, 36)  \n",
       "\n",
       "[1000 rows x 25 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def value2bin(x,cutoffs,col):    \n",
    "    '''\n",
    "    将变量的值转换成相应的组。\n",
    "    x: 需要转换到分组的值\n",
    "    cutoffs: 各组的起始值。\n",
    "    col: 需要分组的列名\n",
    "    return: x对应的组，如group1。从group1开始。\n",
    "    '''    \n",
    "    #切分点从小到大排序。\n",
    "    cutoffs = sorted(cutoffs)\n",
    "    num_bins = len(cutoffs)    \n",
    "    #异常情况：小于第一组的起始值。这里直接放到第一组。    \n",
    "    #异常值建议在分组之前先处理妥善。\n",
    "    cutoffs = [float('-inf')] + cutoffs + [np.inf]\n",
    "    for i in range(1, len(cutoffs)):\n",
    "        if cutoffs[i-1] <= x < cutoffs[i]:            \n",
    "            return '%s%s_[%s, %s)' % (col, i, cutoffs[i-1], cutoffs[i]) \n",
    "\n",
    "for bin_d in bin_dict:\n",
    "    col = list(bin_d.keys())[0]\n",
    "    cutoffs = bin_d[col]\n",
    "    data[col+'_Bin'] = data[col].apply(value2bin,args=(cutoffs,col))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.457979Z",
     "start_time": "2019-05-29T07:48:02.430334Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'Age_Bin'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-a7ff35c17601>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mdf_woe\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mcol\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'Age_Bin'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'TaCA_Bin'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ES'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m     \u001b[0mdf_woe\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconcat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdf_woe\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mCalcWOE\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcol\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'y'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      6\u001b[0m \u001b[0mdf_woe\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf_woe\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0mdf_woe\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-11-a4ed2b6eee63>\u001b[0m in \u001b[0;36mCalcWOE\u001b[0;34m(df, col, target)\u001b[0m\n\u001b[1;32m     64\u001b[0m     \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0m返回WOE和IV\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     65\u001b[0m     '''\n\u001b[0;32m---> 66\u001b[0;31m     \u001b[0mtotal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroupby\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcol\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtarget\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     67\u001b[0m     \u001b[0mtotal\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'total'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mtotal\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     68\u001b[0m     \u001b[0mbad\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroupby\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcol\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtarget\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36mgroupby\u001b[0;34m(self, by, axis, level, as_index, sort, group_keys, squeeze, observed, **kwargs)\u001b[0m\n\u001b[1;32m   7630\u001b[0m         return groupby(self, by=by, axis=axis, level=level, as_index=as_index,\n\u001b[1;32m   7631\u001b[0m                        \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgroup_keys\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mgroup_keys\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msqueeze\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msqueeze\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 7632\u001b[0;31m                        observed=observed, **kwargs)\n\u001b[0m\u001b[1;32m   7633\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   7634\u001b[0m     def asfreq(self, freq, method=None, how=None, normalize=False,\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mgroupby\u001b[0;34m(obj, by, **kwds)\u001b[0m\n\u001b[1;32m   2108\u001b[0m         \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'invalid type: {}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2109\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2110\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mklass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mby\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze, observed, **kwargs)\u001b[0m\n\u001b[1;32m    358\u001b[0m                                                     \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    359\u001b[0m                                                     \u001b[0mobserved\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mobserved\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 360\u001b[0;31m                                                     mutated=self.mutated)\n\u001b[0m\u001b[1;32m    361\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    362\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mobj\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/grouper.py\u001b[0m in \u001b[0;36m_get_grouper\u001b[0;34m(obj, key, axis, level, sort, observed, mutated, validate)\u001b[0m\n\u001b[1;32m    576\u001b[0m                 \u001b[0min_axis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgpr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgpr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    577\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 578\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgpr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    579\u001b[0m         \u001b[0;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgpr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mGrouper\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mgpr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkey\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    580\u001b[0m             \u001b[0;31m# Add key to exclusions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'Age_Bin'"
     ]
    }
   ],
   "source": [
    "# import pdb\n",
    "# pdb.set_trace()\n",
    "df_woe = pd.DataFrame()\n",
    "for col in ['Age_Bin', 'TaCA_Bin', 'ES']:\n",
    "    df_woe = pd.concat([df_woe, pd.DataFrame(CalcWOE(data, col, 'y'))])\n",
    "df_woe = df_woe.reset_index()\n",
    "df_woe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.478919Z",
     "start_time": "2019-05-29T07:47:12.425Z"
    }
   },
   "outputs": [],
   "source": [
    "### 计算KS值\n",
    "def KS(df, score, target):\n",
    "    '''\n",
    "    :param df: 包含目标变量与预测值的数据集,dataframe\n",
    "    :param score: 得分或者概率,str\n",
    "    :param target: 目标变量,str\n",
    "    :return: KS值\n",
    "    '''\n",
    "    total = df.groupby([score])[target].count()\n",
    "    bad = df.groupby([score])[target].sum()\n",
    "    all = pd.DataFrame({'total':total, 'bad':bad})\n",
    "    all.index.name = 'a'\n",
    "    # print(all)\n",
    "    all['good'] = all['total'] - all['bad']\n",
    "    all[score] = all.index\n",
    "    # print(all)\n",
    "    all = all.sort_values(by=score,ascending=False)\n",
    "    all.index = range(len(all))\n",
    "    all['badCumRate'] = all['bad'].cumsum() / all['bad'].sum()\n",
    "    all['goodCumRate'] = all['good'].cumsum() / all['good'].sum()\n",
    "    KS = all.apply(lambda x: x.badCumRate - x.goodCumRate, axis=1)\n",
    "    return max(KS)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.480147Z",
     "start_time": "2019-05-29T07:47:12.428Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "data_bin_woe = data.loc[:, ('Age_Bin', 'TaCA_Bin', 'ES', 'y')]\n",
    "def test(x):\n",
    "    print(x)\n",
    "for col in ['Age_Bin', 'TaCA_Bin', 'ES']:\n",
    "    data_bin_woe[col+'_WOE'] = data_bin_woe[col].map(lambda x: df_woe.loc[df_woe['index']==x, 'WOE'].to_list()[0])\n",
    "data_woe = data_bin_woe.loc[:, ('Age_Bin_WOE', 'TaCA_Bin_WOE', 'ES_WOE', 'y')]\n",
    "data_woe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.481265Z",
     "start_time": "2019-05-29T07:47:12.430Z"
    }
   },
   "outputs": [],
   "source": [
    "clf = LogisticRegression()\n",
    "clf.fit(data_woe.iloc[:, 0:3], data_woe.iloc[:, 3])\n",
    "\n",
    "list_coef = list(clf.coef_[0])\n",
    "intercept = clf.intercept_\n",
    "\n",
    "print(list_coef)\n",
    "print(intercept)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.482407Z",
     "start_time": "2019-05-29T07:47:12.432Z"
    }
   },
   "outputs": [],
   "source": [
    "P0 = 50\n",
    "PDO = 10\n",
    "theta = 0.05\n",
    "\n",
    "B = PDO / np.log(2)\n",
    "A = P0 + B * np.log(theta)\n",
    "\n",
    "print('A: %s, B: %s' % (A, B))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.483542Z",
     "start_time": "2019-05-29T07:47:12.434Z"
    }
   },
   "outputs": [],
   "source": [
    "def logodds(x):\n",
    "    logodds = intercept + x['Age_Bin_WOE']*list_coef[0] + x['TaCA_Bin_WOE']*list_coef[1] + x['ES_WOE']*list_coef[2]\n",
    "    return logodds[0]\n",
    "data_woe['logodds'] = data_woe.apply(lambda x: logodds(x), axis=1)\n",
    "data_woe['proba'] = data_woe['logodds'].map(lambda x: x / (1+x))\n",
    "data_woe['proba_y'] = data_woe['proba'].map(lambda x: 1 if x>0.5 else 0)\n",
    "# FP、TP\n",
    "data_woe['FP'] = data_woe.apply(lambda x: 1 if x['y']==1 and x['proba_y']==0 else 0, axis=1)\n",
    "data_woe['TP'] = data_woe.apply(lambda x: 1 if x['y']==0 and x['proba_y']==0 else 0, axis=1)\n",
    "data_woe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.484660Z",
     "start_time": "2019-05-29T07:47:12.436Z"
    }
   },
   "outputs": [],
   "source": [
    "data_sort_proba = data_woe.sort_values('proba')\n",
    "data_sort_proba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.485914Z",
     "start_time": "2019-05-29T07:47:12.438Z"
    }
   },
   "outputs": [],
   "source": [
    "data_sort_proba['bins'] = pd.qcut(data_sort_proba['proba'], 6)\n",
    "data_sort_proba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.487048Z",
     "start_time": "2019-05-29T07:47:12.440Z"
    }
   },
   "outputs": [],
   "source": [
    "data_sort_proba['bins'].value_counts().sort_index().to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-29T07:48:02.488170Z",
     "start_time": "2019-05-29T07:47:12.443Z"
    }
   },
   "outputs": [],
   "source": [
    "total_n = data_sort_proba['y'].sum()\n",
    "total_p = data_sort_proba['y'].count() - total_n\n",
    "print('All Negatives: %s, All Positives: %s' % (total_n, total_p))\n",
    "\n",
    "bins_calc = pd.concat([\n",
    "    data_sort_proba.groupby(['bins'])['proba_y'].count().sort_index().to_frame(),\n",
    "    data_sort_proba.groupby(['bins'])['proba_y'].sum().sort_index().to_frame(),\n",
    "    data_sort_proba.groupby(['bins'])['FP'].sum().sort_index().to_frame(),\n",
    "    data_sort_proba.groupby(['bins'])['TP'].sum().sort_index().to_frame()\n",
    "], axis=1)\n",
    "bins_calc.columns = ['proba_total', 'proba_n', 'FP', 'TP']\n",
    "bins_calc['proba_p'] = bins_calc['proba_total'] - bins_calc['proba_n']\n",
    "bins_calc['TPR'] = bins_calc['TP']*1.0 / bins_calc['proba_p']\n",
    "bins_calc['FPR'] = bins_calc['FP']*1.0 / bins_calc['proba_n']\n",
    "\n",
    "bins_calc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
